#!/bin/bash

if [ $# -lt 1 ]; then
  cat <<EOF
Usage:$(basename "$0") (batch_save|save_trade_cal|save_financial|save_financial_preview|save_stock_basic|save_research_report|save_pe_percentage|save_valuation|save_stock_day) (skip_exists)
EOF
  exit 1
fi

hackdir=$(
  cd $(dirname $0)
  pwd
)

workdir=$(
  cd $(dirname "$hackdir")
  pwd
)

source "$workdir"/venv/bin/activate

export PYTHONPATH="$workdir"

# skip_exists 参数用于 save_financial|save_financial_preview 时是否跳过库中已经存在数据的股票
if [ $# -gt 1 ] && [ "$2" == "skip_exists" ]; then
  skip_exists="--skip-exists"
else
  skip_exists=""
fi

run_save_financial() {
  # 3-5月抓去年年报；3-5月抓今年一季报；8-9月抓中报；10-11月抓三季报
  month=$(date +%m)
  if [ "$month" -ge 3 ] && [ "$month" -le 5 ]; then
    python3 "$workdir"/AStock/ASApp/save_financial.py --report-date "$(date +%Y --date='-1 year')1231" --financial --no-financial-preview $skip_exists
  fi
  if [ "$month" -ge 3 ] && [ "$month" -le 5 ]; then
    python3 "$workdir"/AStock/ASApp/save_financial.py --report-date "$(date +%Y)0331" --financial --no-financial-preview $skip_exists
  fi
  if [ "$month" -ge 8 ] && [ "$month" -le 9 ]; then
    python3 "$workdir"/AStock/ASApp/save_financial.py --report-date "$(date +%Y)0630" --financial --no-financial-preview $skip_exists
  fi
  if [ "$month" -ge 10 ] && [ "$month" -le 11 ]; then
    python3 "$workdir"/AStock/ASApp/save_financial.py --report-date "$(date +%Y)0930" --financial --no-financial-preview $skip_exists
  fi
  if [ "$month" -eq 1 ] || [ "$month" -eq 2 ] || [ "$month" -eq 6 ] || [ "$month" -eq 7 ] || [ "$month" -eq 12 ]; then
    echo "no need to save financial, exit"
  fi
}

run_save_financial_preview() {
  # 3-4月今年一季报；6-7月今年二季报；9-10月三季报；12-3去年年报
  month=$(date +%m)
  if { [ "$month" -ge 1 ] && [ "$month" -le 3 ]; } || [ "$month" -eq 12 ]; then
    python3 "$workdir"/AStock/ASApp/save_financial.py --report-date "$(date +%Y --date="-1 year")1231" --no-financial --financial-preview $skip_exists
  fi
  if [ "$month" -ge 3 ] && [ "$month" -le 4 ]; then
    python3 "$workdir"/AStock/ASApp/save_financial.py --report-date "$(date +%Y)0331" --no-financial --financial-preview $skip_exists
  fi
  if [ "$month" -ge 6 ] && [ "$month" -le 7 ]; then
    python3 "$workdir"/AStock/ASApp/save_financial.py --report-date "$(date +%Y)0630" --no-financial --financial-preview $skip_exists
  fi
  if [ "$month" -ge 9 ] && [ "$month" -le 10 ]; then
    python3 "$workdir"/AStock/ASApp/save_financial.py --report-date "$(date +%Y)0930" --no-financial --financial-preview $skip_exists
  fi
  if [ "$month" -eq 5 ] || [ "$month" -eq 8 ] || [ "$month" -eq 11 ]; then
    echo "no need to save financial preview, exit"
  fi
}

case $1 in
batch_save)
  # stock day
  python3 "$workdir"/AStock/ASApp/save_stock_day.py
  # valuation
  python3 "$workdir"/AStock/ASApp/save_pe_percentage.py --force-re-save
  python3 "$workdir"/AStock/ASApp/save_research_report.py --begin-date $(date +%Y%m%d --date="-1 month") --end-date $(date +%Y%m%d)
  python3 "$workdir"/AStock/ASApp/save_valuation.py --begin-date $(date +%Y%m%d --date="-3 month") --end-date $(date +%Y%m%d) --close-date $(date +%Y%m%d)
  # financial
  run_save_financial_preview
  run_save_financial
  ;;
save_trade_cal)
  python3 "$workdir"/AStock/ASApp/save_trade_cal.py --begin-date "$(date +%Y)0101" --end-date "$(date +%Y)1231"
  ;;
save_financial)
  # 3-5月抓去年年报；3-5月抓今年一季报；8-9月抓中报；10-11月抓三季报
  run_save_financial
  ;;
save_financial_preview)
  # 3-4月今年一季报；6-7月今年二季报；9-10月三季报；12-3去年年报
  run_save_financial_preview
  ;;
save_stock_basic)
  python3 "$workdir"/AStock/ASApp/save_stock_basic.py
  ;;
save_research_report)
  python3 "$workdir"/AStock/ASApp/save_research_report.py --begin-date $(date +%Y%m%d --date="-1 month") --end-date $(date +%Y%m%d)
  ;;
save_pe_percentage)
  python3 "$workdir"/AStock/ASApp/save_pe_percentage.py --force-re-save
  ;;
save_valuation)
  python3 "$workdir"/AStock/ASApp/save_valuation.py --begin-date $(date +%Y%m%d --date="-3 month") --end-date $(date +%Y%m%d) --close-date $(date +%Y%m%d)
  ;;
save_stock_day)
  python3 "$workdir"/AStock/ASApp/save_stock_day.py
  ;;
*)
  echo "Usage:$(basename "$0") (batch_save|save_trade_cal|save_financial|save_financial_preview|save_stock_basic|save_research_report|save_pe_percentage|save_valuation|save_stock_day) (skip_exists)"
  ;;
esac
